﻿#!/bin/python
# -*- coding:utf-8 -*-
# a script sync. wordbook from youdao dict to anki
import sys
import hashlib,time
import httplib, urllib2, cookielib
from xml.dom.minidom import parse, parseString

class Youdao:

    """init login for Youdao"""
    def __init__(self, username,password):
        self.username = username
        self.password = hashlib.md5(password.encode('utf-8')).hexdigest()
        status = {'LOGIN_SUCCESS':'201'}
        loginHost = 'reg.163.com'
        loginPath = '/services/userlogin?username=%s&password=%s&type=1&product=search' \
            % (self.username, self.password)
        self.headers = {
            'User-Agent':'YoudaoDictPro/3.1.0 CFNetwork/548.1.4 Darwin/11.0.0'
        }

        conn = httplib.HTTPConnection(loginHost)
        conn.request('GET', loginPath,None, self.headers)
        response = conn.getresponse()
        txt = response.read().splitlines()
        if txt[0] == '201':
            self.cookies = response.getheader('Set-Cookie') 
            print 'Login Success!'
        else:
            self.cookies = ''
            print '[ERROR]', [i for i in txt]

        response.close()
            
    def syncDict(self, output):
        dictHost = 'dict.youdao.com'
        dictPath = '/wordbook/api?appVer=mdict.3.1.0.iphonepro&id=961aaaebd8fb1d8c65385561e32bf244&model=iPod%20touch&deviceid=49b0d44f517212e77c74d5c67d30d932e6cae827&mid=5.1'
        nowtimestamp = repr(time.time() * 1000).split('.')[0]
        '''
        FORMAT like: 
        <actionlist>
            <type>words</type>
            <localtimestamp>1337502741787</localtimestamp>
            <remlocaltimestamp>1337502741778</remlocaltimestamp>
            <action type="refresh">
                <localtimestamp>1337517090.037</localtimestamp>
                <remlocaltimestamp>0</remlocaltimestamp>
            </action>
        </actionlist>
        '''
        #expected lastest timestamp
        remlocaltimestamp = nowtimestamp 
        #local last updated time
        localtimestamp = '1117217156809' #Sat May 28 2005 02:05:56 GMT+0800   that is OLD enough!;) @TODO :cached

        data='data=%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-8%22%20%3F%3E%3Cactionlist%3E%3Ctype%3Ewords%3C%2Ftype%3E%3Clocaltimestamp%3E' + localtimestamp \
        + '%3C%2Flocaltimestamp%3E%3Cremlocaltimestamp%3E' + remlocaltimestamp \
        + '%3C%2Fremlocaltimestamp%3E%3Caction%20type%3D%22refresh%22%3E%3Clocaltimestamp%3E' + localtimestamp \
        + '%3C%2Flocaltimestamp%3E%3Cremlocaltimestamp%3E' + remlocaltimestamp \
        + '%3C%2Fremlocaltimestamp%3E%3C%2Faction%3E%3C%2Factionlist%3E'

        self.headers['Cookie'] = self.cookies
        self.headers['Content-Type'] = 'application/x-www-form-urlencoded'
        self.headers['User-Agent'] = 'iphonepro'

        conn = httplib.HTTPConnection(dictHost)
        conn.request('POST',dictPath,data,self.headers)
        response = conn.getresponse()
        status,reason = response.status, response.reason
        if status == 200:
            self.dictXML = response.read()
            f = open('dict.xml', 'w')
            f.write(self.dictXML)
            f.close()
        else:
            print '[ERROR]',status, reason

        response.close()
        
        dom =  parseString(self.dictXML)
        
        f = open(output, 'w')
        for item in dom.getElementsByTagName('alter'):
            word = item.getElementsByTagName('word')[0].firstChild.wholeText
            try:
                trans = item.getElementsByTagName('trans')[0].firstChild.wholeText
            except:
                trans = ''

            word = word.strip().encode('utf-8')
            trans = trans.strip().encode('utf-8')
            
            f.write(word + ' , ' + trans)
            f.write('\r\n')
        f.close()
        print 'Fetch Success! check out wordbook.txt'

def main(args):
    if len(args) < 3 or \
    args[1] in ['-u', '--usage', '-h', '--help', '/h', '/help']:
        print 'Usage:\n\t python', args[0], 'your@email.com your_password\n'
        sys.exit(0)
    
    youdao = Youdao(args[1],args[2])
    youdao.syncDict('wordbook.txt')
    
if __name__ == '__main__':
    main(sys.argv)
